package ihm.panel.camera;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;

import ihm.applet.KrakenApplet;
import ihm.panel.KrakenTablePanel;
import ihm.panel.PanelInterface;
import ihm.panel.TablePanel;
import ihm.panel.camera.display.VLCPanel;
import ihm.panel.camera.streaming.StreamPanel;
import ihm.panel.camera.streaming.StreamPlayerPanel;
import ihm.panel.node.NodePanel;
import ihm.panel.site.CipherPanel;

import javax.swing.JPanel;
import javax.swing.JSplitPane;

import bdd.sql.server.bean.Video;

/**
 * Panneau qui gère l'affichage des sous panneaux Vidéo, Flux et Tâches.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class CameraPanel extends JPanel implements PanelInterface {

	private static final long serialVersionUID = 1L;
	private KrakenApplet applet;
	
	private JSplitPane main_jsp;
	private NodePanel node_jp;
	private JPanel card_jp;

	private StreamPlayerPanel flux_jp;
	private VLCPanel vlc_jp;
	private VideoPanel video_jp;
	private VideoHistPanel hist_jp;
	private CipherPanel cipher_jp;
	
	/**
	 * Constructeur.
	 * @param applet
	 */
	public CameraPanel(KrakenApplet applet) {
		this.applet = applet;
		initPanel();
	}

	@Override
	public void initPanel() {

		setLayout(new BorderLayout());
		
		node_jp = new NodePanel(applet, this);
		
		card_jp = new JPanel();
		card_jp.setLayout(new CardLayout());

		flux_jp = new StreamPlayerPanel();
		vlc_jp = new VLCPanel(applet);
		video_jp = new VideoPanel(applet, this);
		hist_jp = new VideoHistPanel(applet, this);
		cipher_jp = new CipherPanel(applet, this);
		
		card_jp.add(new JPanel(), "Default");
		card_jp.add(flux_jp, "Flux");
		card_jp.add(vlc_jp, "Lecteur");
		card_jp.add(video_jp, "Vidéos");
		card_jp.add(hist_jp, "Historique");
		card_jp.add(cipher_jp, "Chiffrement");
		
		main_jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, node_jp, card_jp);
		main_jsp.setDividerLocation(200);
		main_jsp.setEnabled(false);
		add(main_jsp, BorderLayout.CENTER);
	}
	
	/**
	 * Change le panneau affiché.
	 * @param panelName
	 */
	public void setPanel(String panelName) {
		((CardLayout) card_jp.getLayout()).show(card_jp, panelName);

		JPanel panel = getCurrentPanel();
		if(panel instanceof StreamPlayerPanel) {
			if(panel.isShowing()) {
				((StreamPlayerPanel) panel).setIp_cam(node_jp.getCurrentCamera().getCam_ip());
				((StreamPlayerPanel) panel).setIp_site(node_jp.getCurrentSite().getSiteserv_ip());
				panel.revalidate();
			}
		}
		else if(panel instanceof TablePanel) {
			if(panel.isShowing()) {
				if(panel instanceof VideoPanel) {
					((TablePanel) panel).updateTableInfo();
					((VideoPanel) panel).setIp_cam(node_jp.getCurrentCamera().getCam_ip());
				}
				else if(panel instanceof VideoHistPanel) {
					((VideoHistPanel) panel).updateTableInfo();
					((VideoHistPanel) panel).setIp_cam(node_jp.getCurrentCamera().getCam_ip());
				}
				System.out.println("refresh");
			}
		}
		else if(panel instanceof CipherPanel) {
				if(panel.isShowing()) {
				System.out.println("refresh cipher panel");
				((CipherPanel) panel).updateTableInfo();
				((CipherPanel) panel).setSelectedEncryption();
			}
		}
		else if(panel instanceof StreamPlayerPanel) {
			if(panel.isShowing()) {
				((StreamPlayerPanel) panel).setIp_cam(node_jp.getCurrentCamera().getCam_ip());
				((StreamPlayerPanel) panel).setIp_site(node_jp.getCurrentSite().getSiteserv_ip());
			}
		}
	}
	
	/**
	 * Retourne le panneau affiché.
	 * @return
	 */
	public JPanel getCurrentPanel() {
        JPanel currentPanel = null;

        for(Component component : card_jp.getComponents()) {
        	if(component instanceof JPanel && component.isVisible()) {
                currentPanel = (JPanel) component;
            }
        }

        return currentPanel;
    }
	
	/**
	 * Retourne l'arborescence des sites et caméras.
	 * @return
	 */
	public NodePanel getNodePanel() { return this.node_jp; }

	public VLCPanel getVlc_jp() { return vlc_jp; }

}
